﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 渐变.user_Form
{
    public partial class user_editPwd : Form
    {
        public user_editPwd()
        {
            InitializeComponent();
        }

        private void user_editPwd_Load(object sender, EventArgs e)
        {
            this.Dock = DockStyle.Fill;
        }
        public string opassword { get; set; }
        public string opname { get; set; }
        /*原密码的校验 start*/
        private void txtOldPwd_KeyUp(object sender, KeyEventArgs e)
        {
            if (this.opassword == txtOldPwd.Text)
            {
                label1.Text = "密码输入正确";
                label1.ForeColor = System.Drawing.Color.Green;
            }
            else
            {
                //原密码输入不正确
                label1.Text = "原密码输入不正确";
                label1.ForeColor = System.Drawing.Color.Red;

            }
        }
        /*原密码的校验 end*/


        /*输入两次的新密码校验 start*/
        private void txtNewPwd2_KeyUp(object sender, KeyEventArgs e)
        {
            //比较新输入的两次密码
            if (txtNewPwd.Text == txtNewPwd2.Text)
            {
                label2.Text = "两次密码输入一致!";
                label2.ForeColor = System.Drawing.Color.Green;
            }
            else
            {
                //两次密码输入不一致
                label2.Text = "两次密码输入不一致!";
                label2.ForeColor = System.Drawing.Color.Red;
            }
        }
        /*输入两次的新密码校验 end*/


        /*清空 start*/
        public void clear()
        {
            txtOldPwd.Text = "";
            txtNewPwd.Text = "";
            txtNewPwd2.Text = "";
            label1.Text = "";
            label2.Text = "";
        }
        /*清空 end*/

        /*提交按钮 start*/
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (label1.ForeColor == System.Drawing.Color.Green && label2.ForeColor == System.Drawing.Color.Green)
            {
                //可以修改
                string sql = "update uUser set uPassword=@uPassword where uUsername = @uUsername";
                SqlParameter[] ps =
                {
                    new SqlParameter("@uPassword",txtNewPwd.Text),
                    new SqlParameter("@uUsername",this.opname)
                    };
                int res = DBHelper.ExecuteNonQuery(sql, ps);
                if (res > 0)
                {
                    this.ShowSuccessTip("修改成功");
                    clear();
                }
                else
                {
                    this.ShowErrorTip("修改失败");
                    clear();
                }
            }
            else
            {
                if (label1.ForeColor == System.Drawing.Color.Red)
                {
                    this.ShowErrorTip("原密码不正确！");
                }
                if (label2.ForeColor == System.Drawing.Color.Red)
                {
                    this.ShowErrorTip("两次密码不一致！");
                }

            }
        }
        /*提交按钮 end*/
    }
}
